package sd_services

import (
	"fmt"
	"log"
	"xiehui-service/app/models/sd_models"
	"xiehui-service/global"

	"go.uber.org/zap"
)

type stockService struct {
}

var StockService = new(stockService)

// 查询料仓
func (stockService *stockService) StockList(stamp float64, num int64) (stocks []sd_models.Stock, err error) {
	err = global.App.MSACCESS.Ping()
	if err != nil {
		log.Println("ping==>>", err)
		global.App.Log.Error("查询料仓数据失败：", zap.Any("数据库联接", "ping失败"))
		return nil, err
	}
	rows, err := global.App.MSACCESS.Query(fmt.Sprintf("select Top %d * from StorXMater where Stamp > %.10f order by produce asc", num, stamp))
	if err != nil {
		log.Println("query==>>", err)
		global.App.Log.Error("查询料仓数据失败：", zap.Any("Query查询失败", err))
		return nil, err
	}
	defer rows.Close()

	for rows.Next() {
		var stockItem sd_models.Stock

		if err := rows.Scan(&stockItem.ID, &stockItem.Produce, &stockItem.StoreID, &stockItem.Address, &stockItem.Serial, &stockItem.Storage, &stockItem.MaterID, &stockItem.Material, &stockItem.MatSpecs, &stockItem.Stamp); err != nil {
			log.Println("scan==>>", err)
		}
		stocks = append(stocks, stockItem)
	}

	if err := rows.Err(); err != nil {
		log.Println("rows.err==>>", err)
		global.App.Log.Error("查询料仓数据失败：", zap.Any("查询失败", err))
		return nil, err
	}

	return stocks, nil
}
